package cn.gdcp.lkqapp16;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

import jp.co.recruit_lifestyle.android.widget.WaveSwipeRefreshLayout;

public class WaveSwipeActivity extends AppCompatActivity {
    private static  final int MSG_REFRESH = 1001;
    private ArrayList<String> data = new ArrayList<>();
    private ListView listView;
    private WaveSwipeRefreshLayout waveSwipeRefreshLayout;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wave_swipe);

        listView = findViewById(R.id.main_list);
        waveSwipeRefreshLayout = findViewById(R.id.main_swipe);
        final ArrayAdapter<String> adapter = new ArrayAdapter<>(WaveSwipeActivity.this,
                android.R.layout.simple_list_item_1,data);
        listView.setAdapter(adapter);
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == MSG_REFRESH){
                    waveSwipeRefreshLayout.setRefreshing(false);
                    adapter.notifyDataSetChanged();
                    return true;
                }
                return false;
            }
        });

        waveSwipeRefreshLayout.setOnRefreshListener(new WaveSwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i<15; i++){
                            data.add("下拉刷新" + i);
                        }
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e){
                            e.printStackTrace();
                        }
                        handler.sendEmptyMessage(MSG_REFRESH);
                    }
                }).start();
            }
        });

    }
}
